# -*- coding: utf-8 -*-

"""
Date: 2023/2/24 09:42
Author: Darker
"""
import datetime
from typing import Text, List

from chinese_calendar import find_workday, is_workday


def next_work_day() -> datetime.date:
    """查询下一个工作日"""
    return find_workday()


def last_work_day() -> datetime.date:
    """查询上一个工作日"""
    today = datetime.date.today() + datetime.timedelta()
    days = 0
    while True:
        date = today + datetime.timedelta(days=days)
        if is_workday(date):
            break
        else:
            days -= 1
    return date


def today() -> Text:
    return datetime.date.today().strftime("%Y-%m-%d")


def days_diff(day1: Text, day2: Text) -> int:
    """
    计算两个日期的天数差
    :param day1: 第一个日期
    :param day2: 第二个日期
    :return: 两个日期的天数差(取绝对值)
    """
    day1 = datetime.datetime.strptime(day1, "%Y-%m-%d")
    day2 = datetime.datetime.strptime(day2, "%Y-%m-%d")
    return abs((day1 - day2).days)


def calculate_date(startDate: Text, endDate: Text, isIncludeHoliday: bool = False) -> List:
    """
    计算起止日期中的所有日期
    :param startDate: 开始日期
    :param endDate: 结束日期
    :param isIncludeHoliday: 是否包含节假日
    :return:
    """
    startDate = datetime.datetime.strptime(startDate, "%Y-%m-%d")
    endDate = datetime.datetime.strptime(endDate, "%Y-%m-%d")
    if endDate <= startDate:
        endDate = startDate
        if not isIncludeHoliday:
            return [str(endDate)[:10]] if is_workday(endDate) else []
        else:
            return [str(endDate)[:10]]
    daysList = []
    while startDate <= endDate:
        daysList.append(startDate)
        startDate += datetime.timedelta(days=1)
    if isIncludeHoliday:
        return list(map(lambda x: str(x)[:10], daysList))
    else:
        return list(map(lambda x: str(x)[:10], filter(is_workday, daysList)))


if __name__ == '__main__':
    r = calculate_date("2023-03-12", "2023-03-13", False)
    print(r)
